<template>
 <div class="login">
    <vueHead :name="name"></vueHead>
    <div class="container">
    <form novalidate @submit.stop.prevent="submit">
      <md-input-container md-has-password>
        <label>请输入你的码云token</label>
        <md-input type="password" required  v-model='userinfo.access_token'></md-input>
      </md-input-container>
    </form>

    <md-button class="md-raised md-primary" @click="login" >登陆</md-button>
    <a href="https://gitee.com/oauth/authorize?client_id=1edfdf5a28db21dface1acf25951ad8e867fddfec9a58a2a9a3fde9e5b963057&redirect_uri=http%3A%2F%2Flovexzq.oschina.io%2Fmayun%2F&response_type=code">获取您的码云token</a>
    </div>
 </div>
</template>

<script type="text/ecmascript-6">
import vueHead from '../components/headbar'

export default {
  data() {
    return {
      name: '登陆',
      userinfo: {
        access_token: ''
      }
    }
  },
  created() {
    this.getStorage('userToken')

    var code = this.$route.query.code
    if( code) {
      this.postToken(code)
    }
  },
  components: {
    vueHead
  },
  methods: {
    login() {
      localStorage.setItem('userToken', this.userinfo.access_token)
      this.$router.push('/')
    },
    getStorage(storagename) {
      if (localStorage.getItem(storagename)) {
        this.userinfo.access_token = localStorage.getItem(storagename)
      }
    },
    postToken(code) {
      this.axios
        .post('https://gitee.com/oauth/token', {

            grant_type: 'authorization_code',
            code: code,
            client_id:
              '1edfdf5a28db21dface1acf25951ad8e867fddfec9a58a2a9a3fde9e5b963057',
            redirect_uri: 'http://lovexzq.oschina.io/mayun/#/login',
            client_secret: '484ba93f45177d79df5fc4efeeb984a888372ba3f9eb9604d0f6b5e17eeff335'

        })
        .then(Response => {
           this.userinfo.access_token = Response.data.access_token
        })
        .catch(Error => {
          console.log('数据获取失败' + Error)
        })
    }
  }
}
</script>

<style scoped>

</style>
